# -*- coding: UTF-8 -*-

import datetime
import sys
from sqlalchemy import and_,  func, or_
from typing import Dict, List, Any, Generator

from sqlalchemy import Boolean, Column, DateTime, Integer, String, Index, Table, ForeignKey, text, JSON, TEXT, distinct
from sqlalchemy.orm import Session, relationship
from app.monitor_metrics.models.prom_alarm_template import PromAlarmTemplate
from common.database import Base
from common.database import SessionMixin
from common.utils import is_empty
from core.logger import logger


__all__ = ['PromNetworkProbe']

class PromNetworkProbe(Base, SessionMixin):
    '''
    网络探测任务
    '''
    __tablename__ = "prom_network_probe"
    name = Column(String(128), nullable=False, comment='探测名称')
    #http tcp icmp
    module = Column(String(64), nullable=False, comment='探测模型')
    targets = Column(JSON, nullable=False, default={}, comment='探测目标')
    labels = Column(JSON, nullable=False, default={}, comment='自定义标签')
    # true/false
    state = Column(Boolean, default=text('true'), nullable=False, comment='状态')
    comment = Column(String(256), nullable=True, comment='网络探测任务说明')

    network_group_uuid = Column(Integer, ForeignKey('prom_network_group.uuid'), comment='所属网络探测组uuid')

    __table_args__ = (
        Index('idx_prom_network_probe_name', 'name', unique=True),
        {
            'mysql_engine': 'innodb',
            'mysql_charset': 'utf8mb4',
            'mysql_collate': 'utf8mb4_unicode_ci',
            'extend_existing': True,
            'mysql_row_format': 'dynamic'
        }
    )

    def __repr__(self):
        return f"<network_probe_name='{self.name}'=> type:{self.type}'>"



    def object_to_dict(self, db: Session):
        result = {
            key: getattr(self, key)
            for key in self.__mapper__.c.keys()
        }
        result["network_group"] = self.prom_network_group.name
        return result



    @classmethod
    def get_object_info_by_group_uuid(cls, db: Session, network_group_uuid: int):
        probe_infos = db.query(cls).filter(
            cls.network_group_uuid == network_group_uuid
        ).order_by(cls.name.asc()).all()

        result = [
            cls.get_object_info_by_uuid(db, item.uuid)
            for item in probe_infos
        ]

        return result
